package com.bxg.chanaldemo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class ClientChanal {
    public static void main(String[] args) throws IOException {
        //1.通道
        SocketChannel socketChannel = SocketChannel.open();
        //2.连接服务器
        socketChannel.connect(new InetSocketAddress("127.0.0.1",9999));
        //3.打招呼
        //缓冲区
        ByteBuffer byteBuffer = ByteBuffer.wrap("你好啊!我是java学员".getBytes());
        socketChannel.write(byteBuffer);

        //开始读取数据
        //缓冲区
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(1024);
        int len;
        while ((len=socketChannel.read(byteBuffer1))>0){
            byteBuffer1.flip();
            System.out.println(new String(byteBuffer1.array(),0,len));
            byteBuffer1.clear();
        }
        socketChannel.close();


    }
}
